package cn.cyh.queue_service.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置类
 */
@Configuration
@EnableRabbit
public class RabbitMQConfig {

    // 队列名称常量
    public static final String TAKE_NUMBER = "queue.event.take-number";
    public static final String QUEUE_CALL_NOTIFICATION = "queue.call.notification";
    public static final String QUEUE_STATISTICS_UPDATE = "queue.statistics.update";
    public static final String QUEUE_STATUS_CHANGE = "queue.status.change";
    public static final String QUEUE_LOG_MESSAGE = "queue.log.message";
    
    // 交换机名称常量
    public static final String EXCHANGE_TOPIC = "exchange.topic";
    
    // 路由键常量
    public static final String ROUTING_CALL_NOTIFICATION = "notification.call";
    public static final String ROUTING_TAKE_NUMBER = "take-number.event";
    public static final String ROUTING_STATISTICS_UPDATE = "statistics.update";
    public static final String ROUTING_STATUS_CHANGE = "status.change";
    public static final String ROUTING_LOG_MESSAGE = "log.message";

    /**
     * 配置JSON消息转换器
     */
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 自定义RabbitTemplate
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(jsonMessageConverter());
        return template;
    }

    /**
     * 声明主题交换机
     */
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(EXCHANGE_TOPIC);
    }

    /**
     * 声明叫号通知队列
     */
    @Bean
    public Queue callNotificationQueue() {
        return QueueBuilder.durable(QUEUE_CALL_NOTIFICATION).build();
    }

    @Bean
    public Queue takeNumberQueue() {
        return QueueBuilder.durable(TAKE_NUMBER).build();
    }

    /**
     * 声明统计数据更新队列
     */
    @Bean
    public Queue statisticsUpdateQueue() {
        return QueueBuilder.durable(QUEUE_STATISTICS_UPDATE).build();
    }

    /**
     * 声明状态变更队列
     */
    @Bean
    public Queue statusChangeQueue() {
        return QueueBuilder.durable(QUEUE_STATUS_CHANGE).build();
    }
    
    /**
     * 声明日志消息队列
     */
    @Bean
    public Queue logMessageQueue() {
        return QueueBuilder.durable(QUEUE_LOG_MESSAGE).build();
    }

    /**
     * 叫号通知队列绑定关系
     */
    @Bean
    public Binding callNotificationBinding(Queue callNotificationQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(callNotificationQueue).to(topicExchange).with(ROUTING_CALL_NOTIFICATION);
    }

    /**
     * 取号队列绑定关系
     */
    @Bean
    public Binding takeNumberQueueBinding(Queue takeNumberQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(takeNumberQueue).to(topicExchange).with(ROUTING_TAKE_NUMBER);
    }

    /**
     * 统计数据更新队列绑定关系
     */
    @Bean
    public Binding statisticsUpdateBinding(Queue statisticsUpdateQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(statisticsUpdateQueue).to(topicExchange).with(ROUTING_STATISTICS_UPDATE);
    }

    /**
     * 状态变更队列绑定关系
     */
    @Bean
    public Binding statusChangeBinding(Queue statusChangeQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(statusChangeQueue).to(topicExchange).with(ROUTING_STATUS_CHANGE);
    }
    
    /**
     * 日志消息队列绑定关系
     */
    @Bean
    public Binding logMessageBinding(Queue logMessageQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(logMessageQueue).to(topicExchange).with(ROUTING_LOG_MESSAGE);
    }
} 